const app = getApp();
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_iphoneX:app.globalData.is_iphoneX,
    list: [
      {
        id:1,
        deliveryName:"小肖",
        isDefault:1,
        mobile:'13189698113',
        province:"湖南省",
        city:"长沙市",
        district:"岳麓区",
        address:"汽车西站枫林三路102号"
      }
    ],
    currPage: 1,
    perPage: 10,
    total: 0,
    loading: true,
    back_create:0,//1,从快递直发点击进来 2,从自提点点击进来
    theme:app.theme,
    postageDistance:0,
    // shopDistance
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  onPullDownRefresh() {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 2000);
    this.setData({
      loading: true
    })
    this.data.currPage = 1,
    this.listAddress();
  },
  onShow() {
    this.data.loading=true;
    // this.listAddress();
  },
  onReachBottom() {
    
    this.listAddress();

  },

  editAddress(e) {
    let index = e.currentTarget.dataset.index;
    let l_address = this.data.list[index];
    app.functions.setLocalStorage('l_address', l_address);
    wx.navigateTo({ url: '/pages_user/add_address/add_address' });
  },


  listAddress() {
    let that = this;
    let userInfo = app.functions.getLocalStorage('user');
    const shop = app.functions.getLocalStorage("shop");
    if (that.data.loading == false) return;
    let params = {
      mid: userInfo.id
    };
    app.api.listAddress(params, (response) => {
      const  rows  = response.data;
      that.setData({
        isvoer: true
      })
      if (rows.length == 0) {
        that.setData({
          loading: false
        })
      }
      if(rows.length>0){
        that.setData({
          list: rows
        })
      }else{
        that.setData({
          list: []
        })
      }
    }, (error) => {

    })
  },
  addAddress() {
    wx.navigateTo({ url: '/pages_user/add_address/add_address' });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //删除单个信息
  delAddress(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let params = {
      id: that.data.list[index].id,
    };
    Dialog.confirm({
      title: '温馨提示',
      message: '删除将无法恢复，是否继续！'
    }).then(() => {
      app.api.deleteAddressById(params, (response) => {
        that.listAddress();
        if(this.data.back_create==2){
          let l_address = app.functions.getLocalStorage('create_address');
          if(l_address.id==that.data.list[index].id){
            app.functions.removeLocalStorage('create_address');
          }
        }
      }, (error) => {

      })
    }).catch(() => {
      // on cancel
    });
  },
  onChangeDefault(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let params = {
      id: that.data.list[index].id,
      isDefault:that.data.list[index].isDefault==1?0:1
    }
    app.api.updateAddressById(params, (response) => {
      if(response.data==1){
        let arr=that.data.list;
        app.functions.showToast("修改成功!");
        setTimeout(()=>{
          that.listAddress();
        },1500)
      }
    }, (error) => {
      app.functions.showToast(error.msg);
    })
  }
})